package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Cake;
import com.entity.Consumer;
import com.entity.ShopCart;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servers.CakeServers;
import com.servers.ConsumerServers;
import com.servers.ShopCarService;
import com.util.ShopCaiUtil;

@Controller
public class ShopCarController {
	int number = 0;
	@Autowired
	private CakeServers cakeserver;
	@Autowired
	private ShopCarService shopCarService;
	@Autowired
	private ConsumerServers consumerServers;
	/**
	 * 
	 * 烹饪 故事 的 单件商品详情
	 * @param name      商品名称
	 * @param price     商品价格
	 * @param imageName 图片名称
	 * @param num       购物车图标数据
	 * @param session
	 * @param aaa       当前商品的数量
	 * @return
	 * 1 代表 未购买,显示在购物车上.
	 * 2 用户删除该条产品 在购物车上的信息 (可以通过撤销 还原)
	 * 3 代表用户 退出本次 购物体检 ,并且将该商品删除,不可撤销     
	 * 5代表已购买,购物车上不显示,    
	 */
	@ResponseBody
	@RequestMapping("/detailsInfo")
	public String IntoShopCar(String name, double price, String imageName, int num, HttpSession session,
			String buyNum,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		// 获取 session 中的 url
		String curr_name = (String) session.getAttribute("CURR_COM");
		if (curr_name != null) {
			// 用户登录成功. 然后添加到购物车
			ShopCart shopCart=new ShopCart();
			Consumer consumerByName = consumerServers.getConsumerByName(curr_name);
			shopCart.setScnum(Integer.parseInt(buyNum));
			shopCart.setSc_status(1);
			shopCart.setConsumer(consumerByName);
			shopCart.setScname(name);
			System.out.println("~~~~~~~~~转呗开始添加到购物车~~~~~~~"+shopCart);
			List<ShopCart> findAllShopCarByCid = shopCarService.findAllShopCarByCid(consumerByName.getCid(),1);//状态为1标识用户购物车的东西
			boolean temp=false;
			for (ShopCart shopCart2 : findAllShopCarByCid) {
				if (shopCart2.getConsumer().getCid()==consumerByName.getCid()&&shopCart2.getScname().trim().equals(name)) {
					shopCart2.setScnum(shopCart2.getScnum()+Integer.parseInt(buyNum)); // 如果 本人 的购物车里面 原本已经有了本商品.只需要修改一下数量
					shopCarService.updateShopCartNum(shopCart2); // 修改 购物车里面的 本人的数量
					temp=true;
				}
			}
			if (!temp) {
				// 如果本人 原本购物车里面没有. temp为 false .那么直接向购物车表里面添加一条数据 即可
				System.out.println("~~~~~~~~~本人 原本购物车里面没有~直接向购物车表里面添加一条数据~~~~~~~~~~~~~~~~~~~~~");
				int addShopCart = shopCarService.AddShopCart(shopCart); // 判断 是否添加成功
			}
			List<ShopCart> AllShopCarByCid = shopCarService.findAllShopCarByCid(consumerByName.getCid(),1);
			session.setAttribute("CURR_NUM", AllShopCarByCid.size());
			map.put("shopcarnum", AllShopCarByCid.size()); // 页面显示的 给我用户看到的 购物车的数量
			map.put("CURR", "success");
		}else {
			
			map.put("CURR", "nocurr");
			
		}
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	@RequestMapping("/goshopcar")
	public String goshopcar(String name,Model model) {
		//model.addAttribute("name", name+",");
		System.out.println("进入到goshopcar:"+name);
		
		return "shopcart";
	}
	/**
	 * 
	 * @param session 
	 * @param name	 本次 添加到购物车的 name
	 * @param num   本次天道购物的 产品的 数量
	 * @return
	 * 1: 购物车第一次添加
	 * 2: 购物车 添加相同的产品
	 * 3: 购物车  不是第一添加 添加其他的产品
	 * geShopCarSession
		//model.addAttribute("name", name+",");
		System.out.println("进入到添加购物车name:"+name);
		System.out.println("进入到添加购物车carlist:"+carlist);
	 */
	//@ResponseBody
	@RequestMapping("/shopcar")
	public String geShopCarSession(HttpSession session,String name,String num){
		System.out.println("------进入geShopCarSession类- 添加一个session-num-----------"+name+",,,,,"+num);
		// 获取 session 中的 num
		String curr_num=(String) session.getAttribute("CURR_NUM");
		    //int t_curr_num=Integer.parseInt(curr_num);
		// 获取 session 中的 url
		String curr_name=(String) session.getAttribute("CURR_NAME");
		    //int tempNum=Integer.parseInt(num);
		Map<String ,Object> map=new  HashMap<>();
		if (curr_num == null) {
			session.setAttribute("CURR_NUM","1"); 
			curr_name=name+"_"+num+"-";
			session.setAttribute("CURR_NAME", curr_name);
			map.put("CURR_NUM", 1);
			map.put("CURR_NAME", curr_name);
		}else if (curr_num !=null ) {
			ShopCaiUtil.getStr(curr_name, name, num);
			// 当 curr_num 存在不等于null的时候
			// session中的 CURR_NUM 不需要变
			session.setAttribute("CURR_NUM",curr_num); 
			session.setAttribute("CURR_URL", "goshopdemo?url="+name+","+num+"/"+name+","+num+"/");
			map.put("carnum", curr_num);
		}else if (curr_num !=null  ) {
			
		}
		
		Gson gson=new Gson();
		session.setAttribute("shopCarInfo",null);
		return null;
	}
	@ResponseBody
	@RequestMapping("/goshopcart")
	public String goshopcart(String name,Model model,HttpServletRequest request) {
		//model.addAttribute("name", name+",");
		System.out.println("---------------z自动获取购物车的数据,返回json数据------------------");
		// 模拟 默认的三种 购物车
		/*List<String> sList=new ArrayList<>();
		sList.add("葡萄");
		sList.add("提拉米苏");
		sList.add("和风抹茶");*/
		int curr_num=(int) request.getSession().getAttribute("CURR_NUM"); // 获取到 当前的用户姓名
		int curr_cid=(int) request.getSession().getAttribute("CURR_CID"); // 获取到 当前的用户姓名
		List<ShopCart> findAllShopCarByCid = shopCarService.findAllShopCarByCid(curr_cid,1);
		//用来封装整个json
		Map<String, Object> map=new HashMap<>();
		//用来封装内部 infolist的json 数组
		List<Map<String, Object>> infolist=new ArrayList<>();
		for (ShopCart str : findAllShopCarByCid) {
			//根据name查询cake
			Cake cake = cakeserver.getCakeByName(str.getScname());
			//System.out.println(cake);
			
			Map<String, Object> mapinfo=new HashMap<>();
			mapinfo.put("img", "images/Fruits/"+cake.getCakeName()+".png");
			mapinfo.put("name", cake.getCakeName());
			mapinfo.put("title", cake.getCakeName());
			mapinfo.put("color", "彩色");
			mapinfo.put("cm", "130");
			mapinfo.put("price", cake.getCakePrice());
			mapinfo.put("scid", str.getScid());
			mapinfo.put("num", str.getScnum());// 购物车的数量
			
			infolist.add(mapinfo);
		}
		map.put("status", 0);
		map.put("infoList", infolist);
		
		Gson gson=new Gson();
		System.out.println(gson.toJson(map)+"@@@@@@@@@@@@@@@@尝试添加到购物车@@@@@@@@@@@@@");
		return gson.toJson(map);
	}
	
	@ResponseBody
	@RequestMapping("/addshopcartdemo")
	public String addshopcartdemo(String data) {
		System.out.println("DATA:"+data);
		
		Gson gson=new Gson();
		Map<String, List<ShopCart>> fromJson = gson.fromJson(data, new TypeToken<Map<String, List<ShopCart>>>(){}.getType());
		 List<ShopCart> scList=fromJson.get("datas");
		 System.out.println("----------------------------------");
		 for (ShopCart shopCart : scList) {
			System.out.println(shopCart);
		}
		 
		 System.out.println("----------------------------------");
		return "HELLO";
	}
	

	public void setCakeserver(CakeServers cakeserver) {
		this.cakeserver = cakeserver;
	}
	public void setShopCarService(ShopCarService shopCarService) {
		this.shopCarService = shopCarService;
	}

	public void setConsumerServers(ConsumerServers consumerServers) {
		this.consumerServers = consumerServers;
	}

}
